package com.huluhome.controller;

import com.huluhome.constant.DictEnum;
import com.huluhome.domain.AddArticleBo;
import com.huluhome.domain.Article;
import com.huluhome.domain.ArticleQueryModel;
import com.huluhome.domain.Category;
import com.huluhome.service.IArticleService;
import com.huluhome.service.ICategoryService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by k on 2017/9/23.
 */
@Controller
@RequestMapping("backend")
public class AdminController {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        ArticleQueryModel articleQueryModel = new ArticleQueryModel();
        articleQueryModel.setStatus(-1);
        List<Article> articleList = articleService.getArticles(articleQueryModel);
        System.out.println(articleList.size());
        request.setAttribute("articleList", articleList);
        return "admin/index";
    }

    @GetMapping("/add_article")
    public String addAritcle(HttpServletRequest request) {
        List<Category> categories = categoryService.getCategories(DictEnum.SSTATUS_ALL.getIntCodeValue());
        request.setAttribute("categories", categories);
        return "admin/add_article";
    }

    @RequestMapping("/add")
    public String doAddArticle(AddArticleBo article) {
        int result = articleService.addArticle(article);
        return "admin/index";
    }

    @RequestMapping("/article/delete/{articleId}")
    public String deleleteArticle(@PathVariable int articleId) {
        articleService.deleteArticle(articleId);
        return "admin/index";
    }


    /**
     * 类别列表
     * @param request
     * @return
     */
    @RequestMapping("/categories")
    public String getCategories(HttpServletRequest request) {
        List<Category> categories = categoryService.getCategories(DictEnum.SSTATUS_ALL.getIntCodeValue());
        request.setAttribute("categories", categories);
        return "admin/category_list";
    }

    @RequestMapping("/add_category")
    public String addCategory(String categoryName, int status) {
        categoryService.addCategory(categoryName, status);
        return "redirect:/backend/categories ";
    }
}
